<?php

namespace App\Recharge\Api;

use App\Models\InterfaceConfiguration;
use App\Recharge\Bean\ApiNotifyDataBean;
use App\Recharge\Bean\BasicInformation;
use Illuminate\Http\Request;

class Zixuan extends AbstractApi
{
    /**
     * @inheritDoc
     */
    public function rechargeData(BasicInformation $bean): array
    {
        list($usec, $sec) = explode(" ", microtime());
        $cn_time = date('YmdHis', time()) . round($usec * 1000);
        $array = [
            'appId' => $bean->getSupplierNum(),//应用id
            'appSecret' => $bean->getSecretKey(), //商户密钥
            'outOrderId' => $bean->getOrderNo(), //订单号
            'uuid' => $bean->getAccount(), //充值帐号
            'itemId' => $bean->getGoodsId(), //商品编码
            'itemFace' => $bean->getFaceValue(), //商品面值
            'itemPrice' => ($bean->getUnitPrice() * $bean->getRechargeNum() * $bean->getStartCharging()),//商品面值
            'amount' => $bean->getRechargeNum() * $bean->getStartCharging(),//充值数量,
            'callbackUrl' => $bean->getNotifyUrl(), //异步回调地址
            'timestamp' => $cn_time,
            'ext1' => $bean->getIp(),
        ];
        ksort($array);
        $array['sign'] = md5(urldecode(http_build_query($array)));
        unset($array['appSecret']);
        return $array;
    }


    /**
     * @param InterfaceConfiguration $api
     * @param Request $request
     * @return ApiNotifyDataBean
     */
    public function apiNotifyLogic(InterfaceConfiguration $api, Request $request): ApiNotifyDataBean
    {
        $status = $request->input('orderStatus', '');
        $order_sn = $request->input('outOrderId', '');
        return $this->apiNotifyData($order_sn, $status)->setExecute(in_array($status, ['2', '3']))->setRechargeSuccess($status == '2');
    }
}
